package com.hengjun.kingstar.sdk.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

/**
 * Created by YBH on 2017/10/18.
 * 自定义scrollview
 */

public class MyScrollView extends ScrollView {
    //要滑动的布局
    private View innerView;
    private float y;
    private Rect normalRect = new Rect();
    private boolean animationFinish = true;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onFinishInflate() {
        int childCount = getChildCount();
        if (childCount>0) {
            innerView  = getChildAt(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (innerView == null) {
            return super.onTouchEvent(ev);
        } else {
            commonTouchEvent(ev);
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 自定义touch事件处理
     * @param ev
     */
    private void commonTouchEvent(MotionEvent ev) {
        if (animationFinish) {
            int action = ev.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    y = ev.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float preY = y == 0 ? ev.getY() : y;
                    float nowY = ev.getY();
                    int detailY = (int) (preY - nowY);
                    y = nowY;
                    //操作view让它滚动detailY的一半
                    if (isNeedMove()) {
                        //布局改变位置之前，记录一下正常状态的位置
                        if (normalRect.isEmpty()) {
                            normalRect.set(innerView.getLeft(), innerView.getTop(), innerView.getRight(), innerView.getBottom());
                        }
                        innerView.layout(innerView.getLeft(), innerView.getTop() - detailY / 2, innerView.getRight(), innerView.getBottom() - detailY / 2);
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    //布局回滚到原来的位置
                    y = 0;
                    if (isNeedAnimation()) {
                        animationMove();
                    }
                    break;
            }
        }

    }

    /**
     * 判断是否需要回滚
     * @return
     */
    private boolean isNeedAnimation() {
        return !normalRect.isEmpty();
    }

    /**
     * 有动画的回滚到原始位置
     */
    private void animationMove() {
        TranslateAnimation animation = new TranslateAnimation(0,0,0,normalRect.top - innerView.getTop());//打印测试
        animation.setDuration(200);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                animationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                innerView.clearAnimation();
                //正式滚回原位
                innerView.layout(normalRect.left,normalRect.top,normalRect.right,normalRect.bottom);
                normalRect.setEmpty();
                animationFinish = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        innerView.startAnimation(animation);
    }

    /**
     * 判断是否需要滚动
     * @return
     */
    private boolean isNeedMove() {
        int offset = innerView.getMeasuredHeight() - getHeight();
        int scrollY = getScrollY();
        if (scrollY==0||scrollY==offset) {
            return true;
        }
        return false;
    }
}
